package com.yssoft.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈翼东
 * @说明
 * @time 2018/4/29 上午 08:40
 */
public class AppUtil {
    
    /**
     * 返回app要的接送数据格式
     * @param code 0 (失败),1(成功)
     * @param msg 消息
     * @param data 返回的数据
     * @return
     */
    public static Object returnMapJson(String code,String msg,Map<String,Object> data){
        Map<String,Object> returnParam=new HashMap<>();
        Map<String,Object> childrenParam=new HashMap<>();
        childrenParam.put("code",code);
        childrenParam.put("msg",msg);
        childrenParam.put("data",data==null?"null":data);
        returnParam.put("result",childrenParam);
        System.err.println(JSON.toJSONString(childrenParam));
        return JSONArray.toJSON(returnParam);
    }
    
    /**
     * 将list<Map<String,Object>>封装到json
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Object returnListJson(String code,String msg,List<Map<String,Object>> data){
        Map<String,Object> returnParam=new HashMap<>();
        Map<String,Object> childrenParam=new HashMap<>();
        childrenParam.put("code",code);
        childrenParam.put("msg",msg);
        childrenParam.put("data",data==null?"null":data);
        returnParam.put("result",childrenParam);
        System.err.println(JSON.toJSONString(returnParam));
        return JSONArray.toJSON(returnParam);
    }
    
    /**
     * 将list<实体>封装到json
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Object returnListObjectJson(String code,String msg,List<?> data) throws IllegalAccessException {
        Map<String,Object> returnParam=new HashMap<>();
        Map<String,Object> childrenParam=new HashMap<>();
        childrenParam.put("code",code);
        childrenParam.put("msg",msg);
        List<Map<String,Object>> result=new ArrayList<>();
        for (Object o : data) {
            Class className = o.getClass();
            Map<String,Object> param=new HashMap<>();
            for(; className != Object.class ; className = className.getSuperclass()) {
                Field[] fields = className.getDeclaredFields();//获取所有私有字段
                for (Field field : fields) {
                    field.setAccessible(true);
                    param.put(field.getName(), field.get(o)==null?"":field.get(o));
                }
            }
            result.add(param);
        }
        childrenParam.put("data",data==null?"null":result);
        returnParam.put("result",childrenParam);
        return JSONArray.toJSON(returnParam);
    }
    
    /**
     * 将实体封装到json
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Object returnObjectJson(String code,String msg,Object data) throws IllegalAccessException {
        Map<String,Object> returnParam=new HashMap<>();
        Map<String,Object> childrenParam=new HashMap<>();
        childrenParam.put("code",code);
        childrenParam.put("msg",msg);
        Class className = data.getClass();
        Map<String,Object> param=new HashMap<>();
        for(; className != Object.class ; className = className.getSuperclass()) {
            Field[] fields = className.getDeclaredFields();//获取所有私有字段
            for (Field field : fields) {
                field.setAccessible(true);
                param.put(field.getName(), field.get(data)==null?"":field.get(data));
            }
        }

        childrenParam.put("data",data==null?"null":param);
        returnParam.put("result",childrenParam);
        return JSONArray.toJSON(returnParam);
    }
    
    
    
}
